package org.jimmy.tang.generics;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Vector;

/**
 * @author jimmy
 *         泛型类
 */
public class Pair<T, U> {

    public static void main(String... are) {
        testGetMiddle();
        testGenericExtends();
    }

    /**
     * 
     */
    public static void testGenericExtends() {
        List<Integer> listInteger = new ArrayList<Integer>();
        List<String> listString = new ArrayList<String>();
        printCollection(listInteger);
        printCollection(listString);
    }

    /**
     * 
     */
    @SuppressWarnings("unused")
    public static void testExtendsAndSuper() {

        Vector<? extends Number> x = new Vector<Integer>();// 这是正确的

        // Vector<? extends Number> x1 = new Vector<String>();// 这是错误的

        Vector<? super Integer> x2 = new Vector<Number>();// 这是正确的

        // Vector<? super Integer> x3 = new Vector<Byte>();// 这是错误的
    }

    /**
     * 
     * @param collection
     */
    public static void printCollection(Collection<?> collection) {
        for (Object obj : collection) {
            System.out.println(obj);
        }
    }

    /**
     * 
     */
    public static void testGetMiddle() {
        String[] names = { "john", "Bob", "Lily" };
        String middle = Pair.getMiddle(names);
        System.out.println(middle);
    }

    /**
     * 泛型方法
     * @param a
     * @return
     */
    public static <S> S getMiddle(S[] a) {
        return a[a.length / 2];
    }

    /**
     * 泛型必须是实现某个接口的类
     * @param a
     * @return
     */
    public static <W extends GenericObject & GenericInterface & Comparable<?> & Comparator<?>> W min(
            W[] a) {
        return null;
    }

}

abstract class GenericObject {
}

interface GenericInterface {
}

class Employee<T> {
}
